package com.zss.servlet;

import com.zss.bean.Product;
import com.zss.service.ProductService;
import com.zss.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/updproduct" )
public class UpdProductServlet extends HttpServlet {
    private ProductService productService=new ProductServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Product product = new Product();

        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        long id = Long.parseLong(req.getParameter("id"));
        String productName = req.getParameter("productName");
        double price = Double.parseDouble(req.getParameter("price"));
        String description = req.getParameter("description");
        product.setId(id);
        product.setProductName(productName);
        product.setDescription(description);
        product.setPrice(price);
        boolean b = productService.updProduct(product);
        if (b){
            resp.getWriter().print("<script>alert('修改成功!');window.location.href='/index.jsp';</script>");
        }else {
            resp.getWriter().print("<script>alert('修改失败!');window.location.href='/updateProduct.jsp';</script>");
        }
    }
}
